import 'package:flutter_demo/http/product.dart';
import 'package:flutter_demo/models/kv.dart';
import 'package:flutter_demo/models/product_model/product_model.dart';
import 'package:get/get.dart';

import '../../widget/gallery.dart';

class Product_detailsLogic extends GetxController {
  //商品id
  int? productId = Get.arguments["id"] ?? 0;
  
  //商品详情
  ProductModel? productModel;
  
  //Banner 数据
  List<KeyValueModel> bannerItems = [];

  //banner 当前位置
  int bannerCurrentIndex = 0;
  
  //获取商品详情
  _loadProduct()async{
    productModel = await ProductApi.productDetail(productId!);
    if(productModel?.images != null){
      bannerItems = productModel!.images!.map((e)=>KeyValueModel(key: "${e.id}", value: e.src ?? "")).toList();
    }
  }

  _initData() async{
    await _loadProduct();
    update(["product_details"]);
  }

  //banner 切换事件
  void onChangeBanner(int index,_reason){
    bannerCurrentIndex = index;
    update(["product_banner"]);
  }

  @override
  void onInit() {
    super.onInit();
    _initData();
  }


  // 图片浏览
  void onGalleryTap(int index, KeyValueModel item) {
    Get.to(GalleryWidget(
      initialIndex: index,
      items: bannerItems.map<String>((e) => e.value!).toList(),
    ));
  }
}
